import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:flutter/material.dart';

class CommonTextButton extends StatelessWidget {
  final String text;
  final Function(bool isTrueState) onPressedWithState;
  final bool isTrueState;
  final Color? trueColor;
  final Color? falseColor;
  final Color? trueBackgroundColor;
  final Color? falseBackgroundColor;
  final double fontSize;
  final double borderRadius;
  final double borderWidth;
  final EdgeInsetsGeometry padding;

  const CommonTextButton({
    super.key,
    required this.text,
    required this.onPressedWithState,
    required this.isTrueState,
    this.trueColor,
    this.falseColor = Colors.grey,
    this.trueBackgroundColor = Colors.white,
    this.falseBackgroundColor = Colors.white,
    this.fontSize = 14.0,
    this.borderRadius = 8.0,
    this.borderWidth = 1.0,
    this.padding = const EdgeInsets.symmetric(horizontal: 8.0, vertical: 6.0), // 上下边距设为0
  });

  @override
  Widget build(BuildContext context) {
    final defaultTrueColor = context.colorScheme.primary;
    final currentColor = isTrueState 
        ? trueColor ?? defaultTrueColor 
        : falseColor ?? context.colorScheme.primary;
    final currentBackgroundColor = isTrueState 
        ? trueBackgroundColor 
        : falseBackgroundColor;

    return TextButton(
      onPressed: () => onPressedWithState(isTrueState),
      style: ButtonStyle(
        // 添加最小高度约束，使按钮更紧凑
        minimumSize: WidgetStateProperty.all(Size.zero),
        // 移除默认的内边距
        tapTargetSize: MaterialTapTargetSize.shrinkWrap,
        backgroundColor: WidgetStateProperty.all(currentBackgroundColor),
        shape: WidgetStateProperty.all<RoundedRectangleBorder>(
          RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(borderRadius),
            side: BorderSide(
              color: currentColor,
              width: borderWidth,
            ),
          ),
        ),
        padding: WidgetStateProperty.all(padding),
        overlayColor: WidgetStateProperty.all(currentColor.withOpacity(0.1)),
      ),
      child: Padding(
        // 只保留必要的文字内边距
        padding: const EdgeInsets.symmetric(vertical: 2.0), // 上下各2像素的边距
        child: Text(
          text,
          style: TextStyle(
            color: currentColor,
            fontSize: fontSize,
            // 调整文字高度，避免额外空间
            height: 1.0,
          ),
        ),
      ),
    );
  }
}